package com.zeta.eduservice.service.impl;

import com.zeta.eduservice.entity.question.EduCourse;
import com.zeta.eduservice.entity.question.EduCourseDescription;
import com.zeta.eduservice.entity.vo.CourseInfoForm;
import com.zeta.eduservice.entity.vo.CoursePublishVo;
import com.zeta.eduservice.entity.vo.CourseQuery;
import com.zeta.eduservice.entity.vo.front.CourseFrontVo;
import com.zeta.eduservice.entity.vo.front.CourseWebVo;
import com.zeta.eduservice.mapper.EduCourseMapper;
import com.zeta.eduservice.service.EduCourseDescriptionService;
import com.zeta.eduservice.service.EduCourseService;
import com.zeta.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zeta
 * @since 2020-08-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

	@Autowired
	private EduCourseDescriptionService DescriptionService;
	@Override
	public String saveCourseInfo(CourseInfoForm courseInfoForm) {
		 EduCourse course = new EduCourse();	  
		    BeanUtils.copyProperties(courseInfoForm, course);
		    course.setIsDeleted(false);
		    boolean resultCourseInfo = this.save(course);
		    if(!resultCourseInfo){
		        throw new GuliException(20001, "课程信息保存失败");
		    }
		    //保存课程详情信息
		    EduCourseDescription courseDescription = new EduCourseDescription();
		    courseDescription.setDescription(courseInfoForm.getDescription());
		    courseDescription.setId(course.getId());
		    boolean resultDescription = DescriptionService.save(courseDescription);
		    if(!resultDescription){
		        throw new GuliException(20001, "课程详情信息保存失败");
		    }
		    return course.getId();
}
	@Override
	public CourseInfoForm getCourseInfo(String courseId) {
		EduCourse selectById = baseMapper.selectById(courseId);
		EduCourseDescription courseDescription = DescriptionService.getById(courseId);
		CourseInfoForm courseInfoForm = new CourseInfoForm();
		if(selectById!=null) {
			BeanUtils.copyProperties(selectById, courseInfoForm);
		}
		
		courseInfoForm.setDescription(courseDescription.getDescription());
		return courseInfoForm;
	}
	@Override
	public void updateCourseForm(CourseInfoForm courseInfoForm) {
		EduCourse eduCourse = new EduCourse();
		BeanUtils.copyProperties(courseInfoForm, eduCourse);
		int i = baseMapper.updateById(eduCourse);
		if(i==0)
			throw new GuliException(20001,"修改课程失败");
		EduCourseDescription eduCourseDescription = new EduCourseDescription();
		eduCourseDescription.setId(courseInfoForm.getId());
		eduCourseDescription.setDescription(courseInfoForm.getDescription());
		DescriptionService.updateById(eduCourseDescription);
		
	}
	@Override
	public boolean removeById1(Serializable id) {
		
		  Integer result = baseMapper.deleteById(id);
			
	      return null != result && result > 0;
	}
	@Override
	public CoursePublishVo getCoursePublishVoById(String id) {
	    return baseMapper.selectCoursePublishVoById(id);
	}
	@Override
	public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {

		QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("gmt_create");

		if (courseQuery == null){
			baseMapper.selectPage(pageParam, queryWrapper);
			return;
		}

		String title = courseQuery.getTitle();
		String teacherId = courseQuery.getTeacherId();
		String subjectParentId = courseQuery.getSubjectParentId();
		String subjectId = courseQuery.getSubjectId();
		String academyId=courseQuery.getAcademyId();
		if(!StringUtils.checkValNull(academyId)) {
			queryWrapper.eq("academy_id", academyId);
		}
		if (!StringUtils.checkValNull(title)) {
			queryWrapper.like("title", title);
		}

		if (!StringUtils.checkValNull(teacherId) ) {
			queryWrapper.eq("teacher_id", teacherId);
		}

		if (!StringUtils.checkValNull(subjectParentId)) {
			queryWrapper.eq("subject_parent_id", subjectParentId);
		}

		if (!StringUtils.checkValNull(subjectId)) {
			queryWrapper.eq("subject_id", subjectId);
		}

		baseMapper.selectPage(pageParam, queryWrapper);
	}
	  @Override
	    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
	        //2 根据讲师id查询所讲课程
	        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
	        //判断条件值是否为空，不为空拼接
	        if(!StringUtils.checkValNull(courseFrontVo.getSubjectParentId())) { //一级分类
	            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
	        }
	        if(!StringUtils.checkValNull(courseFrontVo.getSubjectId())) { //二级分类
	            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
	        }
	        if(!StringUtils.checkValNull(courseFrontVo.getBuyCountSort())) { //关注度
	            wrapper.orderByDesc("buy_count");
	        }
	        if (!StringUtils.checkValNull(courseFrontVo.getGmtCreateSort())) { //最新
	            wrapper.orderByDesc("gmt_create");
	        }

	        if (!StringUtils.checkValNull(courseFrontVo.getPriceSort())) {//价格
	            wrapper.orderByDesc("price");
	        }

	        baseMapper.selectPage(pageParam,wrapper);

	        List<EduCourse> records = pageParam.getRecords();
	        long current = pageParam.getCurrent();
	        long pages = pageParam.getPages();
	        long size = pageParam.getSize();
	        long total = pageParam.getTotal();
	        boolean hasNext = pageParam.hasNext();//下一页
	        boolean hasPrevious = pageParam.hasPrevious();//上一页

	        //把分页数据获取出来，放到map集合
	        Map<String, Object> map = new HashMap<>();
	        map.put("items", records);
	        map.put("current", current);
	        map.put("pages", pages);
	        map.put("size", size);
	        map.put("total", total);
	        map.put("hasNext", hasNext);
	        map.put("hasPrevious", hasPrevious);

	        //map返回
	        return map;
	    }

	    //根据课程id，编写sql语句查询课程信息
	    @Override
	    public CourseWebVo getBaseCourseInfo(String courseId) {
	        return baseMapper.getBaseCourseInfo(courseId);
	    }

}